package com.smartcampusbackend.controller;

import com.smartcampusbackend.model.GraduationApplication;
import com.smartcampusbackend.service.GraduationService;
import com.smartcampusbackend.model.User;
import com.smartcampusbackend.mapper.UserMapper;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class GraduationController {
    private static final Logger logger = LoggerFactory.getLogger(GraduationController.class);

    @Autowired
    private GraduationService graduationService;

    @Autowired
    private UserMapper userMapper;

    // 学生提交毕业申请
    @PostMapping("/users/me/graduation/apply")
    public ResponseEntity<String> applyForGraduation(@RequestBody GraduationApplication application) {
        String result = graduationService.applyForGraduation(application);
        return ResponseEntity.ok(result);
    }

    // 学生检查毕业申请状态
    @GetMapping("/users/me/graduation/status")
    public GraduationApplication checkGraduationStatus(@RequestParam Long studentId) {
        return graduationService.checkGraduationStatus(studentId);
    }

    // 管理员查看毕业申请列表
    @GetMapping("/admin/graduations")
    public List<Map<String, Object>> listGraduationApplications(
            @RequestParam(defaultValue = "1") int page, 
            @RequestParam(defaultValue = "10") int size) {
        return graduationService.listGraduationApplications(page, size);
    }

    // 管理员批准毕业申请
    @PutMapping("/admin/graduations/{applicationId}/approve")
    public ResponseEntity<String> approveGraduation(@PathVariable String applicationId) {
        String result = graduationService.approveGraduation(Long.valueOf(applicationId));
        return ResponseEntity.ok(result);
    }

    // 管理员拒绝毕业申请
    @PutMapping("/admin/graduations/{applicationId}/reject")
    public ResponseEntity<String> rejectGraduation(
            @PathVariable String applicationId, 
            @RequestBody String rejectReason) {
        String result = graduationService.rejectGraduation(Long.valueOf(applicationId), rejectReason);
        return ResponseEntity.ok(result);
    }
    
    // 管理员拒绝毕业申请（学分不足）
    @PutMapping("/admin/graduations/{applicationId}/reject-credits")
    public ResponseEntity<String> rejectGraduationWithCreditInfo(
            @PathVariable String applicationId, 
            @RequestBody Map<String, Object> rejectInfo) {
        String result = graduationService.rejectGraduationWithCreditInfo(Long.valueOf(applicationId), rejectInfo);
        return ResponseEntity.ok(result);
    }

    // 管理员获取所有毕业申请
    @GetMapping("/admin/graduation-applications")
    public List<Map<String, Object>> getAllGraduationApplications() {
        // 获取原始毕业申请列表
        List<Map<String, Object>> applications = graduationService.listGraduationApplications(0, 10);
        
        // 为每个申请添加学生姓名
        for (Map<String, Object> application : applications) {
            Long studentId = (Long) application.get("studentId");
            if (studentId != null) {
                try {
                    User student = userMapper.selectById(studentId);
                    if (student != null) {
                        application.put("studentName", student.getRealName() != null ? student.getRealName() : student.getUsername());
                    } else {
                        application.put("studentName", "未知学生");
                    }
                } catch (Exception e) {
                    logger.error("获取学生信息失败", e);
                    application.put("studentName", "未知学生");
                }
            } else {
                application.put("studentName", "未知学生");
            }
        }
        
        return applications;
    }
} 